package pers.zh.parking.mvp.presenter;

import android.app.Application;

import com.amap.api.maps.model.LatLng;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.List;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;
import pers.zh.parking.common.BaseObserver;
import pers.zh.parking.common.BaseResponse;
import pers.zh.parking.common.Const;
import pers.zh.parking.common.RxScheduler;
import pers.zh.parking.data.entity.ParkingLot;
import pers.zh.parking.mvp.contract.MapContract;
import pers.zjc.commonlibs.util.ObjectUtils;

@ActivityScope
public class MapPresenter extends BasePresenter<MapContract.Model, MapContract.View> {

    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public MapPresenter(MapContract.Model model, MapContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void loadLotsAround(LatLng latLng) {
        mModel.getLotsAround(latLng.latitude, latLng.longitude, Const.Map.DEFAULT_RADIUS)
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse<List<ParkingLot>>>(mRootView) {

                  @Override
                  public void onSuccess(BaseResponse<List<ParkingLot>> response) {
                      List<ParkingLot> list = response.getData();
                      if (ObjectUtils.isEmpty(list)) {
                          mRootView.showMessage("暂未发现附近停车场");
                      }
                      else {
                          mRootView.showLotAround(list);
                      }
                  }

              });
    }

    /**
     * 根据名字，位置等搜索停车场信息
     * @param lat
     * @param lng
     * @param defaultRadius
     * @param keyWord
     */
    public void searchLots(double lat, double lng, int defaultRadius, String keyWord) {
        mModel.searchLots(lat, lng, defaultRadius, keyWord).compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .retryWhen(new RetryWithDelay(3, 2))
              .subscribe(new BaseObserver<BaseResponse<List<ParkingLot>>>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse<List<ParkingLot>> response) {
                      List<ParkingLot> list = response.getData();
                      if (ObjectUtils.isEmpty(list)) {
                          mRootView.showMessage("暂未搜索到停车场信息");
                      }
                      else {
                          mRootView.showResultList(list);
                      }
                  }

              });
    }
}
